package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.FisheryImage;
import cn.hellochaos.generator.mapper.FisheryImageMapper;
import cn.hellochaos.generator.service.FisheryImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 渔业对应的图片存储信息 服务实现类
* </p>
*
* @author fmy
* @since 2021-07-22
*/
// log类是直接注入的
@Slf4j
@Service
@Transactional
public class FisheryImageServiceImpl extends ServiceImpl<FisheryImageMapper, FisheryImage> implements FisheryImageService {
    @Autowired
    private FisheryImageMapper fisheryImageMapper;

    @Override
    public Page<FisheryImage> listFisheryImagesByPage(int page, int pageSize,String factor) {
        log.info("正在执行分页查询fisheryImage: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        //尽量不用like
        //QueryWrapper<FisheryImage> queryWrapper =  new QueryWrapper<FisheryImage>().like("", factor);

        QueryWrapper<FisheryImage> queryWrapper =  new QueryWrapper<FisheryImage>().eq("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<FisheryImage> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询fisheryImage完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public FisheryImage getFisheryImageById(int id) {
        log.info("正在查询fisheryImage中id为{}的数据",id);
        FisheryImage fisheryImage = super.getById(id);
        log.info("查询id为{}的fisheryImage{}",id,(null == fisheryImage?"无结果":"成功"));
        return fisheryImage;
    }

    @Override
    public int insertFisheryImage(FisheryImage fisheryImage) {
        log.info("正在插入fisheryImage");
        if (super.save(fisheryImage)) {
            log.info("插入fisheryImage成功,id为{}",fisheryImage.getFisheryImageId());
            return fisheryImage.getFisheryImageId();
        } else {
            log.error("插入fisheryImage失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteFisheryImageById(int id) {
        log.info("正在删除id为{}的fisheryImage",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的fisheryImage成功",id);
            return id;
        } else {
            log.error("删除id为{}的fisheryImage失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateFisheryImage(FisheryImage fisheryImage) {
        log.info("正在更新id为{}的fisheryImage",fisheryImage.getFisheryImageId());
        if (super.updateById(fisheryImage)) {
            log.info("更新d为{}的fisheryImage成功",fisheryImage.getFisheryImageId());
            return fisheryImage.getFisheryImageId();
        } else {
            log.error("更新id为{}的fisheryImage失败",fisheryImage.getFisheryImageId());
            throw new BizException("更新失败[id=" + fisheryImage.getFisheryImageId() + "]");
        }
    }

    @Override
    public String getImagePathByImageId(int id) {
        //to do,测试无结果是否返回null,应该是，之前的测试返回就是null
        FisheryImage fisheryImage = fisheryImageMapper.selectById(id);
        //增加健壮性
        if(fisheryImage==null) {
            return "";
        }
        return fisheryImageMapper.selectById(id).getFisheryImagePath();
    }

//    @Override
//    public String getImagePathById(String id) {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.select("fisheryImagePath").eq("fisheryImageId",id);
//        FisheryImage fisheryImage = (FisheryImage)fisheryImageMapper.selectList(queryWrapper).get(0);
//        return fisheryImage.getFisheryImagePath();
//    }

    @Override
    public String getImagePathByFisheryIdAndImageType(int fisheryId,int imageType) {
        //这里必须用泛型T,否则下方结果集需要强转并且无法使用get,set方法
        QueryWrapper<FisheryImage> queryWrapper = new QueryWrapper();
        /**
         * 多条件查询
         * 例1: allEq({id:1,name:"老王",age:null})--->id = 1 and name = '老王' and age is null
         * 例2: allEq({id:1,name:"老王",age:null}, false)--->id = 1 and name = '老王'
         * {fisheryId:fisheryId,imageType:imageType}
         */

        Map<String,Integer> queryParams = new HashMap<>(16);
        //这里要写字段名而非映射名
        queryParams.put("fishery_id", fisheryId);
        queryParams.put("image_type", imageType);
        //select("fisheryImagePath").
        queryWrapper.allEq(queryParams);
        // 不用强转
        try {
            FisheryImage fisheryImage = fisheryImageMapper.selectList(queryWrapper).get(0);
            return fisheryImage.getFisheryImagePath();
        }
        catch(Exception e){

        }
        return "";
        //return fisheryImage.getFisheryImagePath();
    }

}
